library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.2
ggplot(cars)

ggplot(cars) + aes(x = speed, y = dist)

ggplot(cars) + aes(x = speed, y = dist) + geom_point()

ggplot(cars) + aes(x = speed, y = dist) + geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(cars) + aes(x = speed, y = dist) + geom_point()+geom_smooth() + theme_bw() + labs(title = "Speed Distance Relationship Between Cars", subtitle = "Random survey 2020")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(cars) + aes(x = speed, y = dist) + geom_point()+geom_smooth() + theme_bw() + labs(title = "Speed Distance Relationship Between Cars", subtitle = "Random survey 2020", x = "Speed (mph)", y = "Distance (m)", caption = "dataset :cars")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

url <- "https://bioboot.github.io/bimm143_S20/class-material/up_down_expression.txt"
genes <- read.delim(url)
head(genes)
##         Gene Condition1 Condition2      State
## 1      A4GNT -3.6808610 -3.4401355 unchanging
## 2       AAAS  4.5479580  4.3864126 unchanging
## 3      AASDH  3.7190695  3.4787276 unchanging
## 4       AATF  5.0784720  5.0151916 unchanging
## 5       AATK  0.4711421  0.5598642 unchanging
## 6 AB015752.4 -3.6808610 -3.5921390 unchanging
nrow(genes)
## [1] 5196
ncol(genes)
## [1] 4
colnames(genes)
## [1] "Gene"       "Condition1" "Condition2" "State"
summary(genes)
##      Gene             Condition1        Condition2         State          
##  Length:5196        Min.   :-3.6809   Min.   :-3.5921   Length:5196       
##  Class :character   1st Qu.:-3.6809   1st Qu.:-3.5921   Class :character  
##  Mode  :character   Median :-0.9439   Median :-0.8552   Mode  :character  
##                     Mean   : 0.1800   Mean   : 0.2796                     
##                     3rd Qu.: 4.0859   3rd Qu.: 4.0437                     
##                     Max.   :13.1733   Max.   :12.8731
table(genes$state)
## < table of extent 0 >
ggplot(data = genes) + aes(x = Condition1,y = Condition2) + geom_point()

p <- ggplot(data = genes) + aes(x = Condition1,y = Condition2, col = State) + geom_point()
p

p<-p + scale_color_manual(values = c("blue", "gray", "red"))
p

p <- p + labs(title = "Gene Expression Changes Upon Drug Treatment", y = "Drug Treatment", x = "Control (no drug)"
)
p

library(gapminder)
## Warning: package 'gapminder' was built under R version 4.0.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
gapminder_2007 <- gapminder %>% filter(year == 2007)
ggplot(gapminder_2007) + aes(y = lifeExp, x = gdpPercap) + geom_point()

ggplot(gapminder_2007) + aes(y = lifeExp, x = gdpPercap) + geom_point(alpha = 0.4)

ggplot(gapminder_2007) + aes(y = lifeExp, x = gdpPercap, color = continent, size = pop) + geom_point(alpha = 0.4)

ggplot(gapminder_2007) + aes(y = lifeExp, x = gdpPercap, color = pop) + geom_point(alpha = 0.8)

ggplot(gapminder_2007) + aes(y = lifeExp, x = gdpPercap, size = pop) + geom_point(alpha = 0.4)

ggplot(gapminder_2007) + aes(y = lifeExp, x = gdpPercap, size = pop) + geom_point(alpha = 0.4) + scale_size_area(max_size = 10)

gapminder_1957 <- gapminder %>% filter(year == 1957)
ggplot(gapminder_1957) + aes(x = gdpPercap, y = lifeExp, color = continent, size = pop) + scale_size_area(max_size = 15) + geom_point(alpha = 0.7)

gapminder_1957 <- gapminder %>% filter(year == 1957 | year == 2007)
ggplot(gapminder_1957) + aes(x = gdpPercap, y = lifeExp, color = continent, size = pop) + scale_size_area(max_size = 15) + geom_point(alpha = 0.7) + facet_wrap(~year)

gapminder_top5 <- gapminder %>% filter(year == 2007) %>% arrange(desc(pop)) %>% top_n(5, pop)
gapminder_top5
## # A tibble: 5 × 6
##   country       continent  year lifeExp        pop gdpPercap
##   <fct>         <fct>     <int>   <dbl>      <int>     <dbl>
## 1 China         Asia       2007    73.0 1318683096     4959.
## 2 India         Asia       2007    64.7 1110396331     2452.
## 3 United States Americas   2007    78.2  301139947    42952.
## 4 Indonesia     Asia       2007    70.6  223547000     3541.
## 5 Brazil        Americas   2007    72.4  190010647     9066.
ggplot(gapminder_top5) + geom_col(aes(x = country, y= pop))

ggplot(gapminder_top5) + geom_col(aes(x = country, y = lifeExp))

ggplot(gapminder_top5) + geom_col(aes(x = country, y = lifeExp, fill = continent))

ggplot(gapminder_top5) + geom_col(aes(x = country, y= pop, fill = continent))

ggplot(gapminder_top5) + geom_col(aes(x = country, y= pop, fill = lifeExp))

ggplot(gapminder_top5) + geom_col(aes(x = country, y= pop, fill = gdpPercap))

ggplot(gapminder_top5) + geom_col(aes(x = reorder(country, -pop), y= pop, fill = gdpPercap))

ggplot(gapminder_top5) + geom_col(aes(x = reorder(country, -pop), y= pop, fill = country), col = "gray30") + guides(fill = FALSE)
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

head(USArrests)
##            Murder Assault UrbanPop Rape
## Alabama      13.2     236       58 21.2
## Alaska       10.0     263       48 44.5
## Arizona       8.1     294       80 31.0
## Arkansas      8.8     190       50 19.5
## California    9.0     276       91 40.6
## Colorado      7.9     204       78 38.7
USArrests$State <- rownames(USArrests)
ggplot(USArrests) + aes(x = reorder(State, Murder), y = Murder) + geom_col() + coord_flip()

ggplot(USArrests) + aes(x = reorder(State, Murder), y = Murder) + geom_point() + geom_segment(aes(x = State, xend = State, y = 0, yend = Murder), color = "blue") + coord_flip()

library(gapminder)
library(gifski)
## Warning: package 'gifski' was built under R version 4.0.2
library(png)
## Warning: package 'png' was built under R version 4.0.2
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.0.2
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  shadow_wake(wake_length = 0.1, alpha = FALSE)

library(patchwork)
## Warning: package 'patchwork' was built under R version 4.0.2
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
p4 <- ggplot(mtcars) + geom_bar(aes(carb))
(p1 | p2 | p3) /
  p4
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

sessionInfo()
## R version 4.0.0 (2020-04-24)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS  10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] patchwork_1.1.1 gganimate_1.0.7 png_0.1-7       gifski_1.4.3-1 
## [5] dplyr_1.0.7     gapminder_0.3.0 ggplot2_3.3.5  
## 
## loaded via a namespace (and not attached):
##  [1] progress_1.2.2    tidyselect_1.1.1  xfun_0.29         purrr_0.3.4      
##  [5] splines_4.0.0     lattice_0.20-45   colorspace_2.0-2  vctrs_0.3.8      
##  [9] generics_0.1.0    htmltools_0.5.2   yaml_2.2.1        mgcv_1.8-37      
## [13] utf8_1.2.2        rlang_0.4.11      jquerylib_0.1.4   pillar_1.6.3     
## [17] glue_1.4.2        withr_2.4.2       DBI_1.1.1         tweenr_1.0.2     
## [21] plyr_1.8.6        lifecycle_1.0.1   stringr_1.4.0     munsell_0.5.0    
## [25] gtable_0.3.0      evaluate_0.14     labeling_0.4.2    knitr_1.36       
## [29] fastmap_1.1.0     fansi_0.5.0       highr_0.9         Rcpp_1.0.7       
## [33] scales_1.1.1      farver_2.1.0      hms_1.1.1         digest_0.6.28    
## [37] stringi_1.7.5     grid_4.0.0        cli_3.0.1         tools_4.0.0      
## [41] magrittr_2.0.1    tibble_3.1.5      crayon_1.4.1      pkgconfig_2.0.3  
## [45] ellipsis_0.3.2    Matrix_1.3-4      prettyunits_1.1.1 assertthat_0.2.1 
## [49] rmarkdown_2.11    rstudioapi_0.13   R6_2.5.1          nlme_3.1-153     
## [53] compiler_4.0.0